package scheduler;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class LinkRepository implements ILinkRepository 
{
	
	private BlockingQueue<String> _usedLinkList=new LinkedBlockingQueue<String>(); 
	private BlockingQueue<String> _toParseLinkQueue=new LinkedBlockingQueue<String>();
	
	public void init(List<String> toParselinkList)
	{
		this._toParseLinkQueue.addAll(toParselinkList);
	}
	
	
	public void saveLinks(List<String> linklist)
	{
		for(String s:linklist){
			if(!this._usedLinkList.contains(s) && !this._toParseLinkQueue.contains(s)) 
			{
					this._toParseLinkQueue.offer(s);
					System.out.println("Dodano link do bazy linków:" + s);
			}
		}
	}

	
	public String getLink()
	{
		String link=null;
		try {
			link=this._toParseLinkQueue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this._usedLinkList.add(link);
		return link;
	}
	
	public synchronized void resetRegistry(){
		for(String s:this._usedLinkList){
			this._toParseLinkQueue.add(s);
		}
		this._usedLinkList.clear();
	}

	@Override
	public void destroy() {
		
	}
}
